
#include "ScreenManager.h"


using namespace std;
using namespace cgGame;


CScreenManager::CScreenManager(
							   HWND hwnd, 
							   LPDIRECT3DDEVICE9 d3dDevice)
:m_hWnd(hwnd), 
	m_pD3DDevice(d3dDevice),
	m_pFont(NULL),
	m_pFontRender(NULL),
	m_pDrawGrid(NULL),
	m_pHunterSprite(NULL),
	m_pBulletSprite(NULL),
	m_pBulletHitSprite(NULL),

	m_pHunterBaseSprite(NULL),
	m_pBaseDestoryedSprite(NULL),

	m_pAnimalNormalSprite(NULL),
	m_pAnimalEdibleSprite(NULL),
	m_pAnimalBigSprite(NULL),
	m_pAnimalFastSprite(NULL),
	
	m_pObsWallSprite(NULL),
	m_pObsForestSprite(NULL),
	m_pObsStoneSprite(NULL),
	m_pObsWaterSprite(NULL),

	m_pObsStarSprite(NULL),
	m_pObsBombSprite(NULL),
	m_pObsLifeSprite(NULL),
	m_pObsTimeStillSprite(NULL),

	m_pBgroundSprite(NULL),
	
	m_pCharBornAnimSprite(NULL),
	m_pCharDieAnimSprite(NULL),

	m_pDebug(NULL)
{

};


CScreenManager::~CScreenManager()
{
	SafeDelete(m_pDebug);

	SafeDelete(m_pCharDieAnimSprite);
	SafeDelete(m_pCharBornAnimSprite);

	SafeDelete(m_pBgroundSprite);

	SafeDelete(m_pObsTimeStillSprite);
	SafeDelete(m_pObsLifeSprite);
	SafeDelete(m_pObsBombSprite);
	SafeDelete(m_pObsStarSprite);

	SafeDelete(m_pObsWaterSprite);
	SafeDelete(m_pObsStoneSprite);
	SafeDelete(m_pObsForestSprite);
	SafeDelete(m_pObsWallSprite);
	
	SafeDelete(m_pAnimalFastSprite);
	SafeDelete(m_pAnimalBigSprite);
	SafeDelete(m_pAnimalEdibleSprite);
	SafeDelete(m_pAnimalNormalSprite);

	SafeDelete(m_pBaseDestoryedSprite);
	SafeDelete(m_pHunterBaseSprite);
	SafeDelete(m_pBulletHitSprite);
	SafeDelete(m_pBulletSprite);
	SafeDelete(m_pHunterSprite);

	SafeDelete(m_pDrawGrid);
	SafeDelete(m_pFontRender);
	SafeDelete(m_pFont);

}





bool CScreenManager::InitScreenObjects()
{

	//Font
	m_pFont = new CFont(m_pD3DDevice);
	m_pFont->InitFont("Times New Roman",20,10);
	RECT FPSRect = {0, 0, 300, 100};
	m_pFont->SetFPSRect(FPSRect);
	RECT textRect = {0, 100, 300, 200};
	m_pFont->SetTextRect(textRect);
	m_pFont->SetString("    ");
	//font render
	m_pFontRender = new CFontRenderer(m_pD3DDevice, 24);

	//grid
	m_pDrawGrid = new CDrawGrid(m_pD3DDevice);
	m_pDrawGrid->InitGrid(g_cellSpace, g_gameAreaWidth, g_gameAreaHeight);
	
	//game state instance
	m_pGameState = CGameState::GetGameStateInstance();

	//char born anim sprite
	m_pCharBornAnimSprite = new CD3DXSprite(m_pD3DDevice);
	m_pCharBornAnimSprite->SetTextureScale("res\\pic\\charborn.png", g_obstacleSize, g_obstacleSize);
	m_pCharBornAnimSprite->SetAnimaInfo(4, 0.4f);
	//char die anim sprite
	m_pCharDieAnimSprite = new CD3DXSprite(m_pD3DDevice);
	m_pCharDieAnimSprite->SetTextureScale("res\\pic\\chardie.png", g_obstacleSize, g_obstacleSize);
	m_pCharDieAnimSprite->SetAnimaInfo(8, 0.1f);

	//back ground sprite
	m_pBgroundSprite = new CD3DXSprite(m_pD3DDevice);
	m_pBgroundSprite->SetTextureScale("res\\menu\\graybackground.png", 
		g_screenWidth - g_gameAreaWidth, g_screenHeight);
	m_pBgroundSprite->SetPosition(cgMath::cgVector2D((g_screenWidth + g_gameAreaWidth) / 2, 
		g_screenHeight / 2));

	//bullet sprite
	m_pBulletSprite = new CD3DXSprite(m_pD3DDevice);
	m_pBulletSprite->SetTextureScale("res\\pic\\bullet.png", g_bulletSize, g_bulletSize);
	//bullet hit sprite
	m_pBulletHitSprite = new CD3DXSprite(m_pD3DDevice);
	m_pBulletHitSprite->SetTextureScale("res\\pic\\bullethit.png", g_bulletSize, g_bulletSize);

	//hunter sprite
	m_pHunterSprite = new CD3DXSprite(m_pD3DDevice);
	m_pHunterSprite->SetTextureScale("res\\pic\\hunter.png", g_hunterSize, g_hunterSize);	

	//hunter base sprite
	m_pHunterBaseSprite = new CD3DXSprite(m_pD3DDevice);
	m_pHunterBaseSprite->SetTextureScale("res\\pic\\hunterbase.png", g_obstacleSize, g_obstacleSize);
	//base destroyed sprite
	m_pBaseDestoryedSprite = new CD3DXSprite(m_pD3DDevice);
	m_pBaseDestoryedSprite->SetTextureScale("res\\pic\\basedestroyed.png", g_obstacleSize, g_obstacleSize);

	//animal normal sprite
	m_pAnimalNormalSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalNormalSprite->SetTextureScale("res\\pic\\animal.png", g_animalSize, g_animalSize);
	//animal edible sprite
	m_pAnimalEdibleSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalEdibleSprite->SetTextureScale("res\\pic\\animaledible.png", g_animalSize, g_animalSize);
	//animal big sprite
	m_pAnimalBigSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalBigSprite->SetTextureScale("res\\pic\\animalbig.png", g_animalSize, g_animalSize);
	//animal fase sprite
	m_pAnimalFastSprite = new CD3DXSprite(m_pD3DDevice);
	m_pAnimalFastSprite->SetTextureScale("res\\pic\\animalfast.png", g_animalSize, g_animalSize);

	//forest sprite
	m_pObsForestSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsForestSprite->SetTextureScale("res\\pic\\forest.png", g_obstacleSize, g_obstacleSize);
	//stone sprite
	m_pObsStoneSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsStoneSprite->SetTextureScale("res\\pic\\stone.png", g_cellSpace, g_cellSpace);
	//wall sprite
	m_pObsWallSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsWallSprite->SetTextureScale("res\\pic\\wall.png", g_cellSpace, g_cellSpace );
	//m_pObsWallSprite->SetTextureScale("res\\pic\\wall.png", 
	//	static_cast<int >((*(m_pGameState->GetObstacleListBegin()))->GetObjRange() * 2 ),
	//	static_cast<int >((*(m_pGameState->GetObstacleListBegin()))->GetObjRange() * 2) );
	//water sprite
	m_pObsWaterSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsWaterSprite->SetTextureScale("res\\pic\\water.png", g_obstacleSize, g_obstacleSize);

	//star sprite
	m_pObsStarSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsStarSprite->SetTextureScale("res\\pic\\star.png", g_obstacleSize, g_obstacleSize);
	m_pObsStarSprite->SetAnimaInfo(4, 0.4f);
	//life sprite
	m_pObsLifeSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsLifeSprite->SetTextureScale("res\\pic\\life.png", g_obstacleSize, g_obstacleSize);
	m_pObsLifeSprite->SetAnimaInfo(8, 0.4f);
	//time still sprite
	m_pObsTimeStillSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsTimeStillSprite->SetTextureScale("res\\pic\\timestill.png", g_obstacleSize, g_obstacleSize);
	m_pObsTimeStillSprite->SetAnimaInfo(4, 0.4f);
	//bomb sprite
	m_pObsBombSprite = new CD3DXSprite(m_pD3DDevice);
	m_pObsBombSprite->SetTextureScale("res\\pic\\bomb.png", g_obstacleSize, g_obstacleSize);
	m_pObsBombSprite->SetAnimaInfo(4, 0.4f);

	//debug
	m_pDebug = new CDebug();

	return true;
}



void CScreenManager::UpdatePositionData()
{

}



void CScreenManager::RenderSlideBar(float timeDelta)
{
	float slidePosX = g_gameAreaWidth + g_cellSpace / 2;
	float slidePosY = g_cellSpace;

	m_pBgroundSprite->Render(timeDelta);

	for (int i=0; i<m_pGameState->GetNumAnimalLeft(); ++i)
	{
		slidePosX = static_cast<float> (g_gameAreaWidth) 
			+ 2 * g_cellSpace + (i % 4) * 2 * g_cellSpace;
		slidePosY = static_cast<float> (g_cellSpace)
			+ ((int)(i / 4)) * 2 * g_cellSpace;
		m_pAnimalNormalSprite->SetPosition(cgMath::cgVector2D(slidePosX, slidePosY));
		m_pAnimalNormalSprite->SetRotateAngle(0.0f);
		m_pAnimalNormalSprite->Render(timeDelta);
	}

	std::string levelStr = 
			"Total Score:" + cgMath::FloatToString((float)m_pGameState->GetGameScore()) +
			"\nGame Level : " + cgMath::FloatToString((float)m_pGameState->GetGameLevel()) +
			"\nLifes :      " + cgMath::FloatToString((float)m_pGameState->GetHunterLifes());
	m_pFontRender->Render(D3DXVECTOR3(0.0f, 0.0f, 0.0f), levelStr, true, 
		D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f), true, 
		D3DXVECTOR2(g_gameAreaWidth + g_cellSpace, g_gameAreaHeight - 8 * g_cellSpace));
}



void CScreenManager::RenderObjects(float timeDelta)
{
	if (Game_CurrState_Playing != m_pGameState->GetGamecurrState())
	{
		return;
	}

	m_pDrawGrid->Render(timeDelta);
	RenderSlideBar(timeDelta);

	//UpdatePositionData();

	//hunter
	for (list<CCharacter*>::iterator iter = m_pGameState->GetCharacterListBegin();
		iter != m_pGameState->GetCharacterListEnd();
		++iter)
	{
		if (Character_Type_Hunter == (*iter)->GetCharType())
		{
			m_pHunterSprite->SetPosition((*iter)->GetObjPos());
			m_pHunterSprite->SetRotateAngle(D3DX_PI / 2 * ((*iter)->GetDirection() - 1 ));
			m_pHunterSprite->Render(timeDelta);
		}
	}

	//animal
	for (list<CCharacter*>::iterator iter = m_pGameState->GetCharacterListBegin();
		iter != m_pGameState->GetCharacterListEnd();
		++iter)
	{
		if (Character_Type_Hunter == (*iter)->GetCharType() ||
			false == (*iter)->GetIsAlive())
		{
			continue;
		}

		//animal normal
		if (Character_Type_Animal_Normal == (*iter)->GetCharType())
		{
			m_pAnimalNormalSprite->SetPosition((*iter)->GetObjPos());
			m_pAnimalNormalSprite->SetRotateAngle(D3DX_PI / 2 * ((*iter)->GetDirection() - 1 ));
			m_pAnimalNormalSprite->Render(timeDelta);
		}
		else if (Character_Type_Animal_Eat == (*iter)->GetCharType())
		{
			m_pAnimalEdibleSprite->SetPosition((*iter)->GetObjPos());
			m_pAnimalEdibleSprite->SetRotateAngle(D3DX_PI / 2 * ((*iter)->GetDirection() - 1 ));
			m_pAnimalEdibleSprite->Render(timeDelta);
		}
		else if (Character_Type_Animal_Big == (*iter)->GetCharType())
		{
			m_pAnimalBigSprite->SetPosition((*iter)->GetObjPos());
			m_pAnimalBigSprite->SetRotateAngle(D3DX_PI / 2 * ((*iter)->GetDirection() - 1 ));
			m_pAnimalBigSprite->Render(timeDelta);
		}
		else if (Character_Type_Animal_Fast == (*iter)->GetCharType())
		{
			m_pAnimalFastSprite->SetPosition((*iter)->GetObjPos());
			m_pAnimalFastSprite->SetRotateAngle(D3DX_PI / 2 * ((*iter)->GetDirection() - 1 ));
			m_pAnimalFastSprite->Render(timeDelta);
		}

		//m_pDebug->LogMsg("Pos:  %f", (*iter)->GetObjPos().x);
	}

	//bullet
	for (list<CCharacter*>::iterator iter = m_pGameState->GetBulletListBegin();
			iter != m_pGameState->GetBulletListEnd();
			++iter)
	{
		if (true == (*iter)->GetIsAlive())
		{
			m_pBulletSprite->SetPosition((*iter)->GetObjPos());
			m_pBulletSprite->Render(timeDelta);
		}
		else
		{
  			m_pBulletHitSprite->SetPosition((*iter)->GetObjPos());
			m_pBulletHitSprite->Render(timeDelta);
		}
	}

	//obstacle
	for (list<CObstacle*>::iterator iter = m_pGameState->GetObstacleListBegin();
			iter != m_pGameState->GetObstacleListEnd();
			++iter)
	{
		if (Obstacle_Type_Forest == (*iter)->GetObstacleType())
		{
			m_pObsForestSprite->SetPosition((*iter)->GetObjPos());
			m_pObsForestSprite->Render(timeDelta);
		}
		else if (Obstacle_Type_Stone == (*iter)->GetObstacleType())
		{
			m_pObsStoneSprite->SetPosition((*iter)->GetObjPos());
			m_pObsStoneSprite->Render(timeDelta);
		}
		else if (Obstacle_Type_Wall == (*iter)->GetObstacleType())
		{
			m_pObsWallSprite->SetPosition((*iter)->GetObjPos());
			m_pObsWallSprite->Render(timeDelta);
		}
		else if (Obstacle_Type_Water == (*iter)->GetObstacleType())
		{
			m_pObsWaterSprite->SetPosition((*iter)->GetObjPos());
			m_pObsWaterSprite->Render(timeDelta);
		}
		else if (Obstacle_Type_Base == (*iter)->GetObstacleType())
		{
			if (Game_CurrState_Lose == m_pGameState->GetGamecurrState())
			{
				m_pBaseDestoryedSprite->SetPosition((*iter)->GetObjPos());
				m_pBaseDestoryedSprite->Render(timeDelta);
			}
			else
			{
				m_pHunterBaseSprite->SetPosition((*iter)->GetObjPos());
				m_pHunterBaseSprite->Render(timeDelta);
			}
		}
		else if (Obstacle_Type_Star == (*iter)->GetObstacleType())
		{
			m_pObsStarSprite->SetPosition((*iter)->GetObjPos());
			m_pObsStarSprite->Render(timeDelta);
		}
		else if (Obstacle_Type_Life == (*iter)->GetObstacleType())
		{
			m_pObsLifeSprite->SetPosition((*iter)->GetObjPos());
			m_pObsLifeSprite->Render(timeDelta);
		}
		else if (Obstacle_Type_TimeStill == (*iter)->GetObstacleType())
		{
			m_pObsTimeStillSprite->SetPosition((*iter)->GetObjPos());
			m_pObsTimeStillSprite->Render(timeDelta);
		}
		else if (Obstacle_Type_Bomb == (*iter)->GetObstacleType())
		{
			m_pObsBombSprite->SetPosition((*iter)->GetObjPos());
			m_pObsBombSprite->Render(timeDelta);
		}
	}
	////based on game state
	//if (Game_CurrState_Lose == m_pGameState->GetGamecurrState())
	//{
	//	
	//}

	//animation
	for (list<Animation_Info>::iterator iter = m_pGameState->GetAnimationListBegin();
			iter != m_pGameState->GetAnimationListEnd();
			++iter)
	{
		if (Animation_Type_CharDie == (*iter).type)
		{
			m_pCharDieAnimSprite->SetPosition((*iter).pos);
			m_pCharDieAnimSprite->Render(timeDelta);
		}
		else if (Animation_Type_CharBorn == (*iter).type)
		{
			m_pCharBornAnimSprite->SetPosition((*iter).pos);
			m_pCharBornAnimSprite->Render(timeDelta);
		}
	}

	//font
	m_pFont->RenderFPS(timeDelta);
	m_pFont->RenderText(timeDelta);

	//m_pCharBornAnimSprite->SetPosition(cgMath::cgVector2D(200.0f, 200.0f));
	//m_pCharBornAnimSprite->SetRotateAngle(D3DX_PI / 2);
	//m_pCharBornAnimSprite->Render(timeDelta);

	m_pDebug->LogMsg("%d", m_pGameState->GetNumCharacters());
	//m_pDebug->LogMsg("%d", cgMath::RandomIntBetween(6, 9));

}